On Error Resume Next 
 
strComputer = "."
'Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Dim sDBUser
Dim sDBPwd
Dim sDBServer
Dim sDBName
Dim nbbddsaved, ColCounter, annee, mois, jour, datejour
Set oSQLServer = CreateObject("SQLDMO.SQLServer")
Set oBackup = CreateObject("SQLDMO.Backup")
nbbddsaved = 0

'mise en forme datejour YYYYMMDD
annee = Year(Date)
mois = Month(Date)
jour = Day(date)
If mois >= 10 Then
	If jour >= 10 Then
		datejour = annee & mois & jour
	Else
		datejour = annee & mois & "0" & jour
	End If
Else
	If jour >= 10 Then
		datejour = annee & "0" & mois & jour
	Else
		datejour = annee & "0" & mois & "0" & jour
	End If
end If

'-------- DEBUT PARAMETRAGE ----------
sDBUser = "idclic" 'utilisateur avec droits sysadmin dans SQL
sDBPwd = "Idc;77170-" 'password
sDBServer = ".\SAGEPAIE" 'Instance SQL
'sDBName = "P_CECOMETAL" 'Base
backupPath = "\\Sage-tsebck\archives-mensuelles-sagepaie\" 'destination des fichiers .BAK
'--------  FIN PARAMETRAGE  ----------

'Connexion au serveur SQL
oSQLServer.LoginTimeout = 30
oSQLServer.LoginSecure = True
oSQLServer.Connect sDBServer, sDBUser, sDBPwd

'Listage des bases dans l'instance spécifiée
For Each oDB in oSQLServer.Databases 'pour debug: wscript.echo oDB.Name & " " & oDB.CreateDate & " " & oDB.SpaceAvailableInMb
	sDBName = oDB.Name
	'exclusion des bases systèmes et DS et création du backup
	if (sDBName <> "master") AND (sDBName <> "model") AND (sDBName <> "msdb") AND (sDBName <> "tempdb") then ' AND (right(sDBName,3) <> "_DS") then 
		oBackUp.Initialize = "TRUE" ' overwrite
		oBackup.Database = sDBName
		oBackup.Action = SQLDMOBackup_Database
		oBackup.Files = backupPath & datejour & "_" & sDBName & ".bak"
		oBackup.SQLBackup oSQLServer
		nbbddsaved = nbbddsaved + 1
	end if
Next

'déconnexion serveur sql
oSQLServer.Close()
''''wscript.echo nbbddsaved & " Backup"